package com.wf.ew.storage.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.model.User;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.storage.biz.ProductStorageBiz;
import com.wf.ew.storage.model.ProductStorage;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2019-11-18 12:37:27
 * @version ：1.0
 */
@Repository
public class ProductStorageBizImpl extends BaseDao implements ProductStorageBiz {

	@Override
	public String list(PageResult<T> page, ProductStorage productStorage, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<ProductStorage> rm = new BeanPropertyRowMapper<ProductStorage>(ProductStorage.class);

		StringBuffer sql = new StringBuffer("select p.id,p.companyId");
        sql.append(",p.warehouseName,p.productName,p.specification,p.lengths,p.num");
		sql.append(",p.liWeight,p.liTotalWeight,p.price,p.model");
		sql.append(",p.locality,p.orderNum,p.negative,p.remark,p.createTime,p.createUser");
		sql.append(",p.updateTime,p.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_product_storage p ");
        sql.append(" inner join sys_user suser on suser.id=p.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=p.companyId ");
        sql.append(" where p.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and p.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(productStorage)){
			if(Utils.isNotEmpty(productStorage.getWarehouseName())){
				sql.append(" and (instr(p.warehouseName,:warehouseName)>0 or instr(p.productName,:warehouseName)>0 " +
						"or instr(p.specification,:warehouseName)>0) ");
				map.put("warehouseName", productStorage.getWarehouseName());
			}
			if(Utils.isNotEmpty(productStorage.getSort())&&Utils.isNotEmpty(productStorage.getOrder())){
				sql.append(" order by "+productStorage.getSort()+" "+productStorage.getOrder());
			} else {
				sql.append(" order by orderNum asc");
			}
		}
		PageResult<T> psList = getPageQueryList(page, sql.toString(), map, rm);

		sql = new StringBuffer("select ROUND(sum(p.liTotalWeight),3) liTotalWeight ");
		sql.append(" from we_product_storage p ");
		sql.append(" inner join sys_user suser on suser.id=p.createUser ");
		sql.append(" inner join we_company cmy on cmy.id=p.companyId ");
		sql.append(" where p.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
			sql.append(" and p.companyId=:tablecompanyId ");
			map.put("tablecompanyId", loginUser.getCompanyId());
		}
		if(Utils.isNotEmpty(productStorage)){
			if(Utils.isNotEmpty(productStorage.getWarehouseName())){
				sql.append(" and (instr(p.warehouseName,:warehouseName)>0 or instr(p.productName,:warehouseName)>0 " +
						"or instr(p.specification,:warehouseName)>0) ");
				map.put("warehouseName", productStorage.getWarehouseName());
			}
			if(Utils.isNotEmpty(productStorage.getSort())&&Utils.isNotEmpty(productStorage.getOrder())){
				sql.append(" order by "+productStorage.getSort()+" "+productStorage.getOrder());
			} else {
				sql.append(" order by orderNum asc");
			}
		}
		getMapParameter(sql, map);
		List<ProductStorage> list = jdbcTemplate.query(sql.toString(),map, rm);
		JSONObject totalRow = null;
		if(list != null && list.size() > 0) {
			totalRow = new JSONObject();
			ProductStorage p =list.get(0);
			totalRow.put("liTotalWeight", p.getLiTotalWeight());
		}
		JSONObject data = jsonPage2(psList);
		data.put("totalRow",totalRow);
		return data.toString();
	}

	@Override
	public List<ProductStorage> list(ProductStorage productStorage, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<ProductStorage> rm = new BeanPropertyRowMapper<ProductStorage>(ProductStorage.class);

        StringBuffer sql = new StringBuffer("select p.id,p.companyId");
		sql.append(",p.warehouseName,p.productName,p.specification,p.lengths,p.num");
		sql.append(",p.liWeight,p.liTotalWeight,p.price,p.model");
		sql.append(",p.locality,p.orderNum,p.negative,p.remark,p.createTime,p.createUser");
		sql.append(",p.updateTime,p.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_product_storage p ");
        sql.append(" inner join sys_user suser on suser.id=p.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=p.companyId ");
		sql.append(" where p.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and p.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
		if(Utils.isNotEmpty(productStorage)){
			if(Utils.isNotEmpty(productStorage.getWarehouseName())){
				sql.append(" and (instr(p.warehouseName,:warehouseName)>0 or instr(p.productName,:warehouseName)>0 " +
						"or instr(p.specification,:warehouseName)>0) ");
				map.put("warehouseName", productStorage.getWarehouseName());
			}
			if(Utils.isNotEmpty(productStorage.getSort())&&Utils.isNotEmpty(productStorage.getOrder())){
				sql.append(" order by "+productStorage.getSort()+" "+productStorage.getOrder());
			} else {
				sql.append(" order by orderNum asc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}


	@Override
	public List<ProductStorage> entity(ProductStorage productStorage, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<ProductStorage> rm = new BeanPropertyRowMapper<ProductStorage>(ProductStorage.class);
		StringBuffer sql = new StringBuffer("select p.id,p.companyId");
		sql.append(",p.warehouseName,p.productName,p.specification,p.lengths,p.num");
		sql.append(",p.liWeight,p.liTotalWeight,p.price,p.model");
		sql.append(",p.locality,p.orderNum,p.negative,p.remark,p.createTime,p.createUser");
		sql.append(",p.updateTime,p.updateUser");
		sql.append(",cmy.name companyName,suser.name createName ");
		sql.append(" from we_product_storage p ");
		sql.append(" inner join sys_user suser on suser.id=p.createUser ");
		sql.append(" inner join we_company cmy on cmy.id=p.companyId ");
		sql.append(" where p.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
			sql.append(" and p.companyId=:tablecompanyId ");
			map.put("tablecompanyId", loginUser.getCompanyId());
		}
		if(Utils.isNotEmpty(productStorage)){
			if(Utils.isNotEmpty(productStorage.getWarehouseName())){
				sql.append(" and p.warehouseName=:warehouseName ");
				map.put("warehouseName", productStorage.getWarehouseName());
			}
			if(Utils.isNotEmpty(productStorage.getProductName())){
				sql.append(" and p.productName=:productName ");
				map.put("productName", productStorage.getProductName());
			}
			if(Utils.isNotEmpty(productStorage.getSpecification())){
				sql.append(" and p.specification=:specification ");
				map.put("specification", productStorage.getSpecification());
			}
			if(Utils.isNotEmpty(productStorage.getLengths())){
				sql.append(" and p.lengths=:lengths ");
				map.put("lengths", productStorage.getLengths());
			}
			if(Utils.isNotEmpty(productStorage.getNegative())){
				sql.append(" and p.negative=:negative ");
				map.put("negative", productStorage.getNegative());
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public ProductStorage getById(ProductStorage productStorage) {
		RowMapper<ProductStorage> rm = new BeanPropertyRowMapper<ProductStorage>(ProductStorage.class);
		Map<String, String> map  = new HashMap<String, String>();

		StringBuffer sql = new StringBuffer("select p.id,p.companyId");
		sql.append(",p.warehouseName,p.productName,p.specification,p.lengths,p.num");
		sql.append(",p.liWeight,p.liTotalWeight,p.price,p.model");
		sql.append(",p.locality,p.orderNum,p.negative,p.remark,p.createTime,p.createUser");
		sql.append(",p.updateTime,p.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_product_storage p ");
        sql.append(" inner join sys_user suser on suser.id=p.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=p.companyId ");
		sql.append(" where p.del=0 and cmy.del=0 and p.id=:id ");
		map.put("id", productStorage.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public List<ProductStorage> ajaxProductStorageNum(ProductStorage productStorage, User loginUser) {
		RowMapper<ProductStorage> rm = new BeanPropertyRowMapper<ProductStorage>(ProductStorage.class);
		Map<String, String> map  = new HashMap<String, String>();

		StringBuffer sql = new StringBuffer("select p.id,p.companyId");
		sql.append(",p.warehouseName,p.productName,p.specification,p.lengths,p.num");
		sql.append(",p.liWeight,p.liTotalWeight,p.price,p.model");
		sql.append(",p.locality,p.orderNum,p.negative,p.remark,p.createTime,p.createUser");
		sql.append(",p.updateTime,p.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_product_storage p ");
        sql.append(" inner join sys_user suser on suser.id=p.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=p.companyId ");
		sql.append(" where p.del=0 and cmy.del=0 ");
		sql.append(" and p.companyId=:tablecompanyId ");
		map.put("tablecompanyId", loginUser.getCompanyId());
		sql.append(" and p.warehouseName=:warehouseName and p.productName=:productName ");
		sql.append(" and p.specification=:specification and p.lengths=:lengths and p.negative=:negative ");
		map.put("warehouseName", productStorage.getWarehouseName());
		map.put("productName", productStorage.getProductName());
		map.put("specification", productStorage.getSpecification());
		map.put("lengths", productStorage.getLengths());
		map.put("negative", productStorage.getNegative());
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public boolean add(ProductStorage productStorage, User loginUser) {
		int count = 1;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into we_product_storage (");
        sql.append("companyId,warehouseName,productName,specification");
		sql.append(",lengths,num,liWeight,liTotalWeight");
		sql.append(",price,model,locality,orderNum,negative,remark");
		sql.append(",createUser) values ( ");
        sql.append(":companyId,:warehouseName,:productName,:specification");
		sql.append(",:lengths,:num,:liWeight,:liTotalWeight");
		sql.append(",:price,:model,:locality,:orderNum,:negative,:remark");
		sql.append(",:createUser)");
        map.put("companyId", loginUser.getCompanyId());
        map.put("warehouseName", productStorage.getWarehouseName());
        map.put("productName", productStorage.getProductName());
        map.put("specification", productStorage.getSpecification());
        map.put("lengths", productStorage.getLengths());
        map.put("num", productStorage.getNum()+"");
        map.put("liWeight", productStorage.getLiWeight());
        map.put("liTotalWeight", productStorage.getLiTotalWeight()+"");
        map.put("price", productStorage.getPrice());
        map.put("model", productStorage.getModel());
        map.put("locality", productStorage.getLocality());
        map.put("orderNum", productStorage.getOrderNum()==null?"100":productStorage.getOrderNum()+"");
        map.put("remark", productStorage.getRemark());
        map.put("negative", productStorage.getNegative());
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(ProductStorage productStorage, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_product_storage set ");
        sql.append("warehouseName=:warehouseName,productName=:productName,specification=:specification ");
		sql.append(",lengths=:lengths,num=:num,liWeight=:liWeight ");
		sql.append(",liTotalWeight=:liTotalWeight,price=:price,model=:model ");
		sql.append(",locality=:locality,orderNum=:orderNum,negative=:negative,remark=:remark,updateTime=now() ");
		sql.append(",updateUser=:updateUser where id=:id ");
        map.put("warehouseName", productStorage.getWarehouseName());
        map.put("productName", productStorage.getProductName());
        map.put("specification", productStorage.getSpecification());
        map.put("lengths", productStorage.getLengths());
        map.put("num", productStorage.getNum()+"");
        map.put("liWeight", productStorage.getLiWeight());
        map.put("liTotalWeight", productStorage.getLiTotalWeight()+"");
        map.put("price", productStorage.getPrice());
        map.put("model", productStorage.getModel());
        map.put("locality", productStorage.getLocality());
        map.put("orderNum", productStorage.getOrderNum()+"");
        map.put("negative", productStorage.getNegative());
        map.put("remark", productStorage.getRemark());
		map.put("updateUser", loginUser.getId());
		map.put("id", productStorage.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean updateNum(ProductStorage productStorage, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_product_storage set ");
		sql.append("num=num+(:num),");
		sql.append("liTotalWeight=liTotalWeight+(:liTotalWeight)," +
				"updateTime=now() ");
		sql.append(",updateUser=:updateUser where id=:id ");
        map.put("num", productStorage.getNum()+"");
        map.put("liTotalWeight", productStorage.getLiTotalWeight()+"");
		map.put("updateUser", loginUser.getId());
		map.put("id", productStorage.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(ProductStorage productStorage, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(productStorage.getIds().length()>0){
			for(String ID : productStorage.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update we_product_storage set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
